package com.pointstar.dp.modules.project.util;

import com.pointstar.dp.common.constant.SystemConstant;
import com.pointstar.dp.common.support.properties.GlobalProperties;
import com.pointstar.dp.modules.project.entity.ProjectRecordEntity;
import com.pointstar.dp.modules.project.entity.ProjectRepairRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 操作实体类的工具类
 * @Author zhukerui
 * @Date 2019年3月30日
 */
@Component
public class EntityUtil {
    private static GlobalProperties globalProperties;

    @Autowired
    public EntityUtil(GlobalProperties globalProperties) {
        EntityUtil.globalProperties = globalProperties;
    }

    /**
     * 由于浏览器安全因素，上传文件会默认产生一个虚拟地址
     * 此处将虚拟地址替换成项目配置的上传文件路径进行保存
     * @param object
     * @return
     */
    public static Object convertEntity(Object object) {
        String uploadLocation = globalProperties.getUploadLocation();
        StringBuilder attachment = new StringBuilder();
        if (object instanceof ProjectRepairRecordEntity){
            attachment.append(((ProjectRepairRecordEntity) object).getAttachment());
        }else if (object instanceof ProjectRecordEntity){
            attachment.append(((ProjectRecordEntity) object).getAttachment());
        }
        //判断上传的文件路径是否带有默认的C:\fakepath\
        if (attachment.indexOf(SystemConstant.DEFAULT_UPLOAD_PATH) >= 0) {
            attachment.replace(attachment.indexOf(SystemConstant.DEFAULT_UPLOAD_PATH), attachment.lastIndexOf("\\") + 1, uploadLocation);
        }
        if (object instanceof ProjectRepairRecordEntity){
            ((ProjectRepairRecordEntity) object).setAttachment(attachment.toString());
        }else if (object instanceof ProjectRecordEntity){
            ((ProjectRecordEntity) object).setAttachment(attachment.toString());
        }
        return object;
    }
}
